package jxcmanager.jxc.controller;


import jxcmanager.jxc.dto.TreeDto;
import jxcmanager.jxc.model.RespBean;
import jxcmanager.jxc.pojo.Menu;
import jxcmanager.jxc.service.IMenuService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author Yao
 * @since 2022-02-09
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private IMenuService menuService;

    /**
     * 返回所有菜单的数据
     * @return
     */
    @ResponseBody
    @RequestMapping("queryAllMenus")
    public List<TreeDto> queryAllMenus(Integer roleId){
        return menuService.queryAllMenus(roleId);
    }

    /**
     * 菜单首页
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return "menu/menu";
    }

    /**
     * 菜单列表查询接口
     * @return
     */
    @ResponseBody
    @RequestMapping("list")
    public Map<String,Object> menuList(){
        return menuService.menuList();
    }

    /**
     * 添加菜单页面
     * @param grade   添加菜单的层级
     * @param pId     点击记录的id
     * @param model
     * @return
     */

    @RequestMapping("addMenuPage")
    public String addMenuPage(Integer grade, Integer pId, Model model){
        model.addAttribute("grade",grade);
        model.addAttribute("pId",pId);
        return "menu/add";
    }

    @RequestMapping("save")
    @ResponseBody
    public RespBean saveMenu(Menu menu){
        menuService.saveMenu(menu);
        return RespBean.success("菜单记录添加成功！");
    }

    @RequestMapping("updateMenuPage")
    public String updateMenuPage(Integer id,Model model){
        model.addAttribute("menu",menuService.getById(id));
        return "menu/update";
    }

    /**
     * 菜单记录更新
     * @param menu
     * @return
     */
    @ResponseBody
    @RequestMapping("update")
    public RespBean update(Menu menu){
        menuService.updateMenu(menu);
        return RespBean.success("菜单记录更新成功！");
    }

    /**
     * 菜单删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public RespBean deleteMenu(Integer id){
        menuService.deleteMenuById(id);
        return RespBean.success("菜单记录删除成功!");
    }


}
